package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.VehicleApply;
import com.amsp.oa.oademo.mapper.VehicleApplyMapper;
import com.amsp.oa.oademo.service.VehicleApplyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Random;
import java.text.SimpleDateFormat;

@Service
public class VehicleApplyServiceImpl extends ServiceImpl<VehicleApplyMapper, VehicleApply> implements VehicleApplyService {

    @Override
    public IPage<VehicleApply> getApplyList(Page<VehicleApply> page, String keyword, String status) {
        LambdaQueryWrapper<VehicleApply> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(keyword)) {
            wrapper.like(VehicleApply::getApplyNo, keyword)
                  .or()
                  .like(VehicleApply::getApplicant, keyword)
                  .or()
                  .like(VehicleApply::getDepartment, keyword);
        }
        
        if (StringUtils.hasText(status)) {
            wrapper.eq(VehicleApply::getStatus, status);
        }
        
        wrapper.orderByDesc(VehicleApply::getCreateTime);
        
        return this.page(page, wrapper);
    }

    @Override
    public VehicleApply submitApply(VehicleApply apply) {
        // 1. 生成申请编号
        String applyNo = generateApplyNo();
        apply.setApplyNo(applyNo);
        
        // 2. 设置初始状态
        apply.setStatus("pending");
        
        // 3. 设置时间
        Date now = new Date();
        apply.setCreateTime(now);
        apply.setUpdateTime(now);
        
        // 4. 数据验证
        validateApply(apply);
        
        // 5. 保存数据
        this.save(apply);
        
        return apply;
    }

    @Override
    public VehicleApply updateApply(VehicleApply apply) {
        apply.setUpdateTime(new Date());
        this.updateById(apply);
        return apply;
    }

    @Override
    public void deleteApply(Long id) {
        this.removeById(id);
    }

    @Override
    public VehicleApply getApplyByNo(String applyNo) {
        LambdaQueryWrapper<VehicleApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VehicleApply::getApplyNo, applyNo);
        return this.getOne(wrapper);
    }

    @Override
    public IPage<VehicleApply> getMyApplies(Page<VehicleApply> page, String applicant) {
        LambdaQueryWrapper<VehicleApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VehicleApply::getApplicant, applicant)
              .orderByDesc(VehicleApply::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public VehicleApply approveApply(Long id, String approver, String approveRemark, boolean isApproved) {
        VehicleApply apply = this.getById(id);
        apply.setApprover(approver);
        apply.setApproveRemark(approveRemark);
        apply.setApproveTime(new Date());
        apply.setStatus(isApproved ? "approved" : "rejected");
        apply.setUpdateTime(new Date());
        this.updateById(apply);
        return apply;
    }

    @Override
    public VehicleApply assignDriver(Long id, String driverName, String vehicleNo) {
        VehicleApply apply = this.getById(id);
        apply.setDriverName(driverName);
        apply.setVehicleNo(vehicleNo);
        apply.setUpdateTime(new Date());
        this.updateById(apply);
        return apply;
    }

    @Override
    public VehicleApply cancelApply(Long id) {
        VehicleApply apply = this.getById(id);
        apply.setStatus("cancelled");
        apply.setUpdateTime(new Date());
        this.updateById(apply);
        return apply;
    }

    private String generateApplyNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        String random = String.format("%03d", new Random().nextInt(1000));
        return "YC" + dateStr + random;
    }

    private void validateApply(VehicleApply apply) {
        if (StringUtils.isEmpty(apply.getApplicant())) {
            throw new IllegalArgumentException("申请人不能为空");
        }
        if (StringUtils.isEmpty(apply.getDepartment())) {
            throw new IllegalArgumentException("申请部门不能为空");
        }
        if (StringUtils.isEmpty(apply.getPhone())) {
            throw new IllegalArgumentException("联系电话不能为空");
        }
        if (StringUtils.isEmpty(apply.getVehicleType())) {
            throw new IllegalArgumentException("车辆类型不能为空");
        }
        if (StringUtils.isEmpty(apply.getPurpose())) {
            throw new IllegalArgumentException("用车事由不能为空");
        }
        if (apply.getPassengerCount() == null || apply.getPassengerCount() < 1) {
            throw new IllegalArgumentException("乘车人数必须大于0");
        }
        if (StringUtils.isEmpty(apply.getDestination())) {
            throw new IllegalArgumentException("目的地不能为空");
        }
        if (apply.getStartTime() == null || apply.getEndTime() == null) {
            throw new IllegalArgumentException("用车时间不能为空");
        }
        if (apply.getEndTime().before(apply.getStartTime())) {
            throw new IllegalArgumentException("结束时间不能早于开始时间");
        }
    }
} 